/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
 /**
 * 检查项目标准字典列表初始化
 */
var SdsStandDicSub = {
    id: "dicSubTree",	//表格id
    seItem: null,		                //选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    dicSubId: 0,
    dicId: 0,
    pId: 0
};

/**
 * 根据标准化字典子项id查找关联检查项目
 */
SdsStandDicSub.onClickDicSub = function (e, treeId, treeNode) {
    if (treeNode.pId == null) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体字典。");
        return false;
    }
    SdsStandDicSub.dicSubId = "";
    SdsStandDicSub.dicId = "";
    SdsStandDicSub.pId = "";
    if (treeNode.level == "1"){
        //标准化字典,加载检查医嘱
        SdsStandDicSub.dicId = treeNode.id;
        SdsStandRisorderMatch.seItem = null;
        SdsStandDicSub.dicSubId = 0;
        var compare = "match";
        SdsStandRisorderMatch.search(compare);
        SdsStandRisitemMatch.search(compare);
    }else {
        //标准化字典子项，加载检查项目字典
        SdsStandDicSub.dicSubId = treeNode.id;
        SdsStandDicSub.dicId = treeNode.pId;
        //查询标准化_检查项目
        SdsStandRisitemMatch.seItem = null;
        var compare = "match";
        SdsStandRisorderMatch.search(compare);
        SdsStandRisitemMatch.search(compare);
    }
}

/************************************************************
 ********************** 右上表格部分*******************************
 ***********************************************************/
/**
 */
/**
 * 标准化_检查医嘱配置管理初始化
 */
var SdsStandRisorderMatch = {
    id: "SdsStandRisorderMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandRisorderMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        //{field: 'selectItem', radio: true,formatter : selectcheckbox},
        {title: '检查医嘱', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '分类', field: 'orderTypeDesc', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 对照
 */
SdsStandRisorderMatch.compare = function (){
    var selected = $('#SdsStandRisorderMatchTable').bootstrapTable('getSelections');
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandRisorderMatch.table.refresh();
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("risitemIds","");
        ajax.set("risorderIds",idStr);
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandRisorderMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandRisorderMatch.table.refresh();
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("risitemIds","");
        ajax.set("risorderIds",row.id + ",");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

$("#SdsStandRisorderMatchTable").on("check-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandRisorderMatch.table.refresh();
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("risitemIds","");
        var risorderIds = "";
        for (var i in dataArr){
            risorderIds += dataArr[i].id + ",";
        }
        ajax.set("risorderIds",risorderIds);
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

/**
 * 撤销
 */
$("#SdsStandRisorderMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandRisorderMatch.table.refresh();
        SdsStandRisitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("risorderIds",row.id + ",");
    ajax.set("risitemIds","");
    ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
    ajax.set("sdsStandDicsubId","");
    ajax.start();
});

$("#SdsStandRisorderMatchTable").on("uncheck-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandRisorderMatch.table.refresh();
        SdsStandRisitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    var risorderIds = "";
    for (var i in dataArr){
        risorderIds += dataArr[i].id + ",";
    }
    ajax.set("risorderIds",risorderIds);
    ajax.set("risitemIds","");
    ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
    ajax.set("sdsStandDicsubId","");
    ajax.start();
});

/**
 * 未对照
 */
SdsStandRisorderMatch.compareNo = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "noMatch";
        SdsStandRisorderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}

/**
 * 已对照
 */
SdsStandRisorderMatch.compareYes = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "match";
        SdsStandRisorderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandRisorderMatch.compareAll = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "all";
        SdsStandRisorderMatch.table.refresh({query: queryData,  pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandRisorderMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/autoCompareRisorder", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandRisorderMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_检查项目配置列表
 */
SdsStandRisorderMatch.search = function (compare) {
    var queryData = {};
    queryData['dicId'] = SdsStandDicSub.dicId
    queryData['compare'] = compare;
    queryData['description'] = $("#seachOrderKey").val();
    SdsStandRisorderMatch.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 查询医嘱关键字
 */
$('#seachOrderKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandRisorderMatch.search();
    }
});


/************************************************************
 ********************** 右下表格部分*******************************
 ***********************************************************/
/**
/**
 * 标准化_检查项目配置管理初始化
 */
var SdsStandRisitemMatch = {
    id: "SdsStandRisitemMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandRisitemMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        //{field: 'selectItem', radio: true,formatter : selectcheckbox},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '检查项目', field: 'description', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        console.log(row)
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 检查是否选中
 */
SdsStandRisitemMatch.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsStandRisitemMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增标准化_检查项目配置
 */
SdsStandRisitemMatch.add = function () {
    var index = Sys.open('新增标准化_检查项目配置',Sys.backendPath + '/sds/sdsstandrisitemmatch/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看标准化_检查项目配置详情
 */
SdsStandRisitemMatch.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('标准化_检查项目配置详情',Sys.backendPath + '/sds/sdsstandrisitemmatch/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除标准化_检查项目配置
 */
SdsStandRisitemMatch.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/delete", function (data) {
	            Sys.success("删除成功!");
	            SdsStandRisitemMatch.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsStandRisitemMatchId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除标准化_检查项目配置?",operation);
    }
};

/**
 * 对照
 */
SdsStandRisitemMatch.compare = function (){
    var selected = $('#SdsStandRisitemMatchTable').bootstrapTable('getSelections');
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        ajax.set("risitemIds",idStr);
        ajax.set("risorderIds","");
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandRisitemMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    console.log(row)
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            /*SdsStandRisorderMatch.table.refresh();*/
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        ajax.set("risitemIds",row.id + ",");
        ajax.set("risorderIds","");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

$("#SdsStandRisitemMatchTable").on("check-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            /*SdsStandRisorderMatch.table.refresh();*/
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        var risitemIds = "";
        for (var i in dataArr){
            risitemIds += dataArr[i].id + ",";
        }
        ajax.set("risitemIds",risitemIds);
        ajax.set("risorderIds","");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

/**
 * 撤销
 */
$("#SdsStandRisitemMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandRisitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("risitemIds",row.id + ",");
    ajax.set("risorderIds","");
    ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
    ajax.set("sdsStandDicId","");
    ajax.start();
});

$("#SdsStandRisitemMatchTable").on("uncheck-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandRisitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    var risitemIds = "";
    for (var i in dataArr){
        risitemIds += dataArr[i].id + ",";
    }
    ajax.set("risitemIds",risitemIds);
    ajax.set("risorderIds","");
    ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
    ajax.set("sdsStandDicId","");
    ajax.start();
});

/**
 * 未对照
 */
SdsStandRisitemMatch.compareNo = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "noMatch";
        SdsStandRisitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}

/**
 * 已对照
 */
SdsStandRisitemMatch.compareYes = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "match";
        SdsStandRisitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandRisitemMatch.compareAll = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "all";
        SdsStandRisitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandRisitemMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/autoCompareRisitem", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandRisitemMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_检查项目配置列表
 */
SdsStandRisitemMatch.search = function (compare) {
    var queryData = {};
    queryData['dicId'] = SdsStandDicSub.dicId;
    queryData['dicSubId'] = SdsStandDicSub.dicSubId;
    queryData['risCode'] = $("#risCode").val();
    queryData['result'] = $("#result").val();
    queryData['compare'] = compare;
    queryData['description'] = $("#seachRisitemKey").val();
    SdsStandRisitemMatch.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 查询检查项目关键字
 */
$('#seachRisitemKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandRisitemMatch.search();
    }
});

$(function () {
    //查询检查字典树
    var ztree = new $ZTree("dicSubTree", Sys.backendPath + "/sds/sdsstanddicsub/tree/" + "影像检查");
    ztree.bindOnClick(SdsStandDicSub.onClickDicSub);
    ztree.init();
    SdsStandDicSub.ztree = ztree;
    fuzzySearch('dicSubTree','#seachKey',null,true);

    //对照的字典颜色改变
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrisitemmatch/matchDicsub", function (data) {
        var treeObj = $.fn.zTree.getZTreeObj("dicSubTree");
        for (var i=0;i<data.length;i++){
            var node = treeObj.getNodeByParam("id", data[i]);
            $("#"+node.tId+"_span").css("color",'red');
        }
    }, function (data) {
        Sys.error("渲染失败!" + data.responseJSON.message + "!");
    });
    ajax.start();

    //检查医嘱
    var risorderColunms = SdsStandRisorderMatch.initColumn();
    var risorderTable = new BSTable(SdsStandRisorderMatch.id, Sys.backendPath + "/sds/sdsstandrisitemmatch/listMatchOrdmast", risorderColunms);
    risorderTable.setPaginationType("server");
    risorderTable.setPageSize(10);
    risorderTable.setSubHeight(-10);
    risorderTable.setShowExport(false);
    risorderTable.setShowRefresh(false);
    risorderTable.setShowToggle(false);
    risorderTable.setShowColumns(false);
    risorderTable.setQueryParams({"compare":0});
    risorderTable.setQueryParams({"dicId":0});
    SdsStandRisorderMatch.table = risorderTable.init();

    //检查项目
    var defaultColunms = SdsStandRisitemMatch.initColumn();
    var table = new BSTable(SdsStandRisitemMatch.id, Sys.backendPath + "/sds/sdsstandrisitemmatch/listExamdic", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-10);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setQueryParams({"dicSubId":0})
    table.setQueryParams({"compare":""});
    SdsStandRisitemMatch.table = table.init();
});
